import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
//封装临时游客身份
import {getUUID} from "@/utils/uuid_token"

const state={
    goodInfo:{},
    //游客临时身份
    uuid_token:getUUID()
}
const mutations={
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo;
    }
}
const actions={
    //获取产品信息
   async getGoodInfo({commit},skuId){
     let result  = await reqGoodsInfo(skuId);
     if(result.code==200){
        commit("GETGOODINFO",result.data);
     }
    },
    //将产品加入购物车
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
     let result = await reqAddOrUpdateShopCart(skuId,skuNum);
     //代表加入购物车成功
     if(result.code == 200){
        return "ok"
     }else{
     //加入购物车失败
        return Promise.reject(new Error('faile'))
     }
    }
}
const getters={
    //路径导航的简化
    categoryView(state){
        return state.goodInfo.categoryView||{}
    },
    //产品信息的简化
    skuInfo(state){
        return state.goodInfo.skuInfo||{}
    },
    //产品售卖的属性变化
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList||{}
    }
}

export default{
    state,
    mutations,
    actions,
    getters,
}